import 'dart:async';
import 'dart:io';

import 'package:common_utils/common_utils.dart';
import 'package:flutter/material.dart';
import 'package:flutter_framework_master/page/DiscoverPage.dart';
import 'package:flutter_framework_master/page/HomePage.dart';
import 'package:flutter_framework_master/page/MessagePage.dart';
import 'package:flutter_framework_master/page/MinePage.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

// 自定有状态组件Tab
class Tabs extends StatefulWidget {
  final index;

  Tabs({Key? key, this.index = 0}) : super(key: key);

  @override
  _TabsState createState() => _TabsState(this.index);
}

class _TabsState extends State<Tabs> {
  int? _currentIndex;
  PageController? _pageController;

  //定时器
  Timer? _timer;
  int msgCount = 99;

  //上次点击时间
  DateTime? _lastPressedAt;

  _TabsState(index) {
    this._currentIndex = index;
  }

  List<Widget> pageList = [];

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    pageList = [HomePage(), DiscoverPage(), MessagePage(), MinePage()];
    this._pageController = PageController(initialPage: this._currentIndex!);
  }

  @override
  void dispose() {
    // TODO: implement dispose
    if (ObjectUtil.isNotEmpty(_timer)) {
      _timer!.cancel();
    }
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return WillPopScope(
      child: Scaffold(
          body: PageView(
            controller: this._pageController,
            onPageChanged: (index) {
              setState(() {
                this._currentIndex = index;
              });
            },
            children: this.pageList,
          ),
          bottomNavigationBar: BottomNavigationBar(
            currentIndex: this._currentIndex!,
            onTap: (int index) {
              setState(() {
                //第4步，设置点击底部Tab的时候的页面跳转
                this._currentIndex = index;
                this._pageController!.jumpToPage(this._currentIndex!);
              });
            },

            iconSize: 25.0,
            type: BottomNavigationBarType.fixed,
            //超过三个以上需要设置该属性
            fixedColor: Color(0xFF3af3db),
            selectedFontSize: 30.sp,
            unselectedFontSize: 30.sp,
            items: [
              BottomNavigationBarItem(
                  icon: Image.asset('images/icon_home_normal.png',
                      width: 55.w, height: 55.w),
                  activeIcon: Image.asset('images/icon_home_select.png',
                      width: 55.w, height: 55.w),
                  label: "首页"),
              BottomNavigationBarItem(
                  icon: Image.asset('images/icon_discover_normal.png',
                      width: 55.w, height: 55.w),
                  activeIcon: Image.asset('images/icon_discover_select.png',
                      width: 55.w, height: 55.w),
                  label: "发现"),
              BottomNavigationBarItem(
                  icon: Stack(
                    children: [
                      Image.asset('images/icon_msg_normal.png',
                          width: 55.w, height: 55.w),
                      Positioned(
                        right: 0,
                        child: msgCount == 0
                            ? Container()
                            : new Container(
                                padding: EdgeInsets.all(2.w),
                                decoration: new BoxDecoration(
                                  color: Colors.red,
                                  borderRadius: BorderRadius.circular(12.r),
                                ),
                                constraints: BoxConstraints(
                                  minWidth: 24.w,
                                  minHeight: 24.w,
                                ),
                                child: new Text(
                                  msgCount > 99
                                      ? "99+"
                                      : msgCount == 0
                                          ? ""
                                          : msgCount.toString(), //通知数量
                                  style: new TextStyle(
                                    color: Colors.white,
                                    fontSize: 16.sp,
                                  ),
                                  textAlign: TextAlign.right,
                                ),
                              ),
                      )
                    ],
                  ),
                  activeIcon: Stack(
                    children: [
                      Image.asset('images/icon_msg_select.png',
                          width: 55.w, height: 55.w),
                      Positioned(
                        right: 0,
                        child: msgCount == null || msgCount == 0
                            ? Container()
                            : Container(
                                padding: EdgeInsets.all(2.w),
                                decoration: new BoxDecoration(
                                  color: Color(0xFFEE6132),
                                  borderRadius: BorderRadius.circular(32.r),
                                ),
                                constraints: BoxConstraints(
                                  minWidth: 24.w,
                                  minHeight: 24.w,
                                ),
                                child: msgCount == null || msgCount == 0
                                    ? Container()
                                    : new Text(
                                        msgCount > 99
                                            ? "99+"
                                            : msgCount == 0
                                                ? ""
                                                : msgCount.toString(), //通知数量
                                        style: new TextStyle(
                                          color: Colors.white,
                                          fontSize: 16.sp,
                                        ),
                                        textAlign: TextAlign.right,
                                      ),
                              ),
                      )
                    ],
                  ),
                  label: "消息"),
              BottomNavigationBarItem(
                  icon: Image.asset('images/icon_mine_normal.png',
                      width: 55.w, height: 55.w),
                  activeIcon: Image.asset('images/icon_mine_select.png',
                      width: 55.w, height: 55.w),
                  label: "我的")
            ],
          )),
      onWillPop: () async {
        if (_lastPressedAt == null ||
            DateTime.now().difference(_lastPressedAt!) > Duration(seconds: 2)) {
          //两次点击间隔超过1秒则重新计时
          _lastPressedAt = DateTime.now();
          // showToast.show('再按一次退出程序');
          return false;
        } else {
          exit(0);
        }
      },
    );
  }
}
